import Vuex from 'vuex'

const createStore = () => {
    return new Vuex.Store({
        state: {
            headTitle: '',
            isSearch: false,
            isClose: false,
            isGoBack: true,
            pageNum: 0,
            userInfo: {},
            menghuiList: [
                {
                    "id": 260,
                    "title": "最近更新查询",
                    menghuiComp: 'UpdateQuery'
                }, {
                    "id": 259,
                    "title": "[画集]萌绘图库画集批量下载[会员专用][更新至22年08月]",
                    menghuiComp: 'Album'
                }, {
                    "id": 258,
                    "title": "[会员特权]教程下载 萌绘图库海量画集一键转存下载",
                    menghuiComp: 'MembershipPrivileges'
                }, {
                    "id": 257,
                    "title": "RE:从零开始的萌绘生活",
                    menghuiComp: 'MenghuiLife'
                }, {
                    "id": 256,
                    "title": "欢迎光临，我来为您带路。",
                    menghuiComp: 'WelCome'
                }, {
                    "id": 255,
                    "title": "新年快乐！新的旅程，萌绘还将继续努力！",
                    menghuiComp: 'NewYear'
                }, {
                    "id": 254,
                    "title": "20150713今天不更新，只为纪念。",
                    menghuiComp: 'NotUpdata'
                }]
        },
        getters: {
        },
        mutations: {
            setHeadTitle(state, value) {
                state.headTitle = value;
            },
            handleIsOpenSearch(state) {
                //导航查询显示
                state.isSearch = !state.isSearch;
            },
            handleClose(state) {
                //侧边导航显示
                state.isClose = !state.isClose;
                if (state.isClose) {
                    document.querySelector("html").scrollTop = 0;
                    document.querySelector('html').style.overflow = "hidden"
                } else {
                    document.querySelector('html').style.overflow = "auto"
                }
            },
            changeGoBack(state, value) {
                //历史返回设置
                state.isGoBack = value;
            },
            changePage(state, app) {
                state.pageNum = parseInt(app.$cookies.get('activePage'))
            },
            CloseNav(state) {
                //侧边导航关闭
                state.isClose = false;
            },
            async getUserInfo(state, app) {
                //获取存储再浏览器里的用户信息
                const res = await app.$api.filesHost()
                // console.log(res.data);
                if (app.$cookies.get('userInfo')) {
                    state.userInfo = app.$cookies.get('userInfo');
                    if (Object.keys(state.userInfo).length > 0) {
                        state.userInfo.imgHost = res.data;
                    }
                }
            },
            async setUserInfo(state, { value, app }) {
                //更新用户信息
                if (Object.keys(value).length>0) {
                    app.$cookies.set("userInfo", value)
                } else {

                    app.$cookies.remove("userInfo")
                }
                const res = await app.$api.filesHost()
                if (Object.keys(value).length > 0) {
                    value.imgHost = res.data
                }
                state.userInfo = value;
            }
        },
        actions: {
            async handleSetActiveArr(conText, { path, app }) {
                // console.log(conText,path,app);
                //面包屑导航
                let pathArr = path.split('/');
                let activePathArr = []
                for (let i = 0; i < pathArr.length; i++) {
                    pathArr[i] = '/' + pathArr[i]
                    const res = await app.$api.getContentTitle({ classifyPath: pathArr[i], isLike: true });
                    if (res.data.length > 0) {
                        activePathArr.push(res.data[0])
                    }
                }
                app.$cookies.set('activePathArr', activePathArr)
            },
            async changePath(conText, value) {
                // 账号设置中心 个人导航
                if (value == '/set/bind') {
                    value = '/set/general'
                }
                return value;
            },
            // async updateUserInfoAvatar(conText,value){
            //   let userInfo=conText.state.userInfo;
            //   const res=await filesHost()
            //   conText.commit("setUserInfo",{...userInfo,avatar:res.data+userInfo.avatar})
            // }
        },
        modules: {
        }
    })
}

export default createStore